package com.imooc.course.business.controller.album;

import com.imooc.course.business.form.MemberCourseForm;
import com.imooc.course.server.dto.MemberCourseDto;
import com.imooc.course.server.dto.ResponseDto;
import com.imooc.course.server.service.MemberCourseService;
import com.imooc.course.server.utils.CopyUtil;
import com.imooc.course.server.utils.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/album/memberCourse")
public class AlbumMemberCourseController {

    private static final Logger log = LoggerFactory.getLogger(AlbumMemberCourseController.class);
    public static final String BUSINESS_NAME = "会员课程报名";


    @Resource(name = "memberCourseService")
    private MemberCourseService memberCourseService;


    /**
     * 会员报名课程
     * @param memberCourseForm
     * @return
     */
    @PostMapping("/enroll")
    public ResponseDto addMemberCourse(@RequestBody MemberCourseForm memberCourseForm) {
        //入参检查
        ValidatorUtil.notNull(memberCourseForm.getMemberId(), "会员id");
        ValidatorUtil.notNull(memberCourseForm.getCourseId(), "课程id");

        MemberCourseDto memberCourseDto = CopyUtil.copy(memberCourseForm, MemberCourseDto.class);
        memberCourseDto = memberCourseService.enroll(memberCourseDto);
        return ResponseDto.success().setContent(memberCourseDto);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto del(@PathVariable String id) {
        memberCourseService.delete(id);
        return ResponseDto.success();
    }

    /**
     * 获取该会员是否已报名课程的信息
     * @param memberCourseForm
     * @return
     */
    @PostMapping("/get-enroll")
    public ResponseDto getEnroll(@RequestBody MemberCourseForm memberCourseForm) {
        //入参检查
        ValidatorUtil.notNull(memberCourseForm.getMemberId(), "会员id");

        MemberCourseDto memberCourseDto = CopyUtil.copy(memberCourseForm, MemberCourseDto.class);
        List<MemberCourseDto> memberCourseDtoList = memberCourseService.getEnroll(memberCourseDto);
        return ResponseDto.success().setContent(memberCourseDtoList);
    }

}
